package com.eyuai.tab


import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.eyuai.tab.databinding.ActivityMainBinding
import com.eyuai.tab.ui.main.SectionsPagerAdapter
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
        val viewPager: ViewPager = binding.viewPager
        viewPager.adapter = sectionsPagerAdapter
        val tabs: TabLayout = binding.tabs
        //必须和Mode是MODE_FIXED才管用，
        // GRAVITY_FILL：平分tablayout的宽度给每个标签
        // GRAVITY_CENTER：所有的标签居中展示，标签的宽度根据标签内容的宽度
        tabs.setTabGravity(TabLayout.GRAVITY_FILL);
        // Mode MODE_FIXED：tablayout的宽度是固定的，如果标签过多，会挤压每个标签，比如title展示为2行，末尾打点
        // MODE_SCROLLABLE：tablayout的宽度不是固定的，如果标签过多可以滚动展示
        tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabs.setTabIndicatorFullWidth(false);
        tabs.setupWithViewPager(viewPager)

        for (i in 0 until tabs.tabCount) {
            var tabView: View = sectionsPagerAdapter.getTabView(i)
            val imageView: View = tabView.findViewById<View>(R.id.iv_tab_red)
            val textView = tabView?.findViewById<TextView>(R.id.tv_tab_title)
            if (i==0){
                textView.setTextColor(getResources().getColor(R.color.black));
            }else{
                textView.setTextColor(getResources().getColor(R.color.white));

            }
            if (i == 3) {
                imageView.visibility = View.VISIBLE
            } else {
                imageView.visibility = View.GONE
            }
            tabs.getTabAt(i)?.setCustomView(tabView);

        }

        tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val position = tab.position
                val customView = tab.customView
                customView?.findViewById<View>(R.id.iv_tab_red)?.setVisibility(View.GONE);
                val textView = customView?.findViewById<TextView>(R.id.tv_tab_title) as TextView
                textView.setTextColor(getResources().getColor(R.color.black));

            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                val customView = tab.customView
                customView?.findViewById<View>(R.id.iv_tab_red)?.setVisibility(View.GONE);
                val textView = customView?.findViewById<TextView>(R.id.tv_tab_title) as TextView
                textView.setTextColor(getResources().getColor(R.color.white));
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

        val fab: FloatingActionButton = binding.fab

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
    }
}